package org.example.base.common.controller;

import org.apache.commons.lang3.ObjectUtils;
import org.example.base.common.service.UserService;
import org.example.core.enums.ApiResult;
import org.example.core.exception.ErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping
public class LoginController {

    @Autowired
    private UserService userService;


    /**
     * 获取登陆token信息
     *
     * @param params
     * @return
     */
    @GetMapping("/login")
    public ApiResult login(@RequestParam Map<String, Object> params) {
        return ApiResult.success();
    }

    /**
     * 注册
     * @param params
     * @return
     */
    @PostMapping("/register")
    public ApiResult register(@RequestBody Map<String, Object> params) {
        Object account = params.get("account");
        Object password = params.get("password");
        if (ObjectUtils.anyNull(account, password)) {
            throw new ErrorException("账号或密码不能为空！");
        }
        userService.register(params);
        return ApiResult.success();
    }

    /**
     * 修改密码
     * @param params
     * @return
     */
    @PostMapping("/modifyPassword")
    public ApiResult modifyPassword(@RequestBody Map<String,Object> params){
        Object password = params.get("password");
        Object id = params.get("userId");
        if (ObjectUtils.anyNull(password,id)){
            throw new ErrorException("密码或用户id不能为空");
        }
        userService.modifyPassword(params);
        return ApiResult.success();
    }
}
